package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.CheckGroupDao;


import com.itheima.health.dao.CheckItemDao;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.result.PageResult;
import com.itheima.health.result.QueryPageBean;

import com.itheima.health.service.CheckGroupService;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;


import java.util.List;
import java.util.Map;

/**
 * 检查组管理
 */

@Service
@Slf4j
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupDao checkGroupDao;
    @Autowired
    private CheckItemDao checkItemDao;


    /**
     * 新增
     *
     * @param checkGroup
     * @param checkitemIds
     */
    @Transactional
    public void add(@RequestBody CheckGroup checkGroup, @RequestParam List<Integer> checkitemIds) {
        log.info("[检查组-添加]data：{}", checkGroup);
        checkGroupDao.add(checkGroup);
        Integer groupId = checkGroup.getId();
        if (checkitemIds != null && checkitemIds.size() > 0) {
            for (Integer checkitemId : checkitemIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("checkgroupId", checkitemId);
                map.put("checkitemId", checkitemId);
                checkGroupDao.setCheckGroupAndCheckItem(map);
            }
        }
    }

    /**
     * 分页查询
     *
     * @param queryPageBean
     * @return
     */
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        //使用分页插件
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        // 分页查询数据库
        Page<CheckGroup> page = checkGroupDao.findByCondition(queryPageBean.getQueryString());
        //封装分页数据
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 根据ID查询
     *
     * @param id
     * @return
     */
    public CheckGroup findById(Integer id) {
        log.info("[检查组-根据ID查询]id:{}", id);
        return checkGroupDao.selectById(id);
    }

    /**
     * 根据id删除
     *
     * @param id
     */
    @Transactional
    public void deleteCheckGroupitemById(Integer id) {
        log.info("[检查组-根据id删除]id：{}", id);
        //TODO 校验是否可以删除
        checkGroupDao.deleteById(id);
    }

    /**
     * 查询所有检查组
     *
     * @return
     */
    public List<CheckItem> findAll() {
        log.info("[检查组-查询所有]~");
        return checkGroupDao.selectAll();
    }


    /**
     * 编辑
     *
     * @param checkGroup
     */
    @Transactional
    public void edit(@RequestBody CheckGroup checkGroup,@RequestParam List<Integer> checkitemIds) {

        checkGroupDao.deleteAssociation(checkGroup.getId());
        setCheckGroupAndCheckItem(checkGroup.getId(),checkitemIds);
        checkGroupDao.edit(checkGroup);

    }

    private void setCheckGroupAndCheckItem(Integer checkGroupId, List<Integer> checkitemIds) {
        if(checkitemIds != null && checkitemIds.size() > 0){
            for (Integer checkitemId : checkitemIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("checkgroup_id", checkGroupId);
                map.put("checkitem_id", checkitemId);
                checkGroupDao.setCheckGroupAndCheckItem(map);
            }
        }
    }

    /**
     * 回显检查项的ids
     *
     * @param id
     * @return
     */
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
        List<Integer> checkItemIds = checkGroupDao.findCheckItemIdsByCheckGroupId(id);
        return checkItemIds;
    }

}